//
//  MyFamilyView.m
//  AILock
//
//  Created by 谭亚明 on 2020/6/22.
//  Copyright © 2020 tym. All rights reserved.
//

#import "MyFamilyView.h"
#import "UIView+ZFFrame.h"
#import "Sizes.h"
#import "ShowAlertView.h"
#import "FamilyService.h"
#import "MyFamilyApplyCell.h"
#import "TYMBaseTableView.h"
#import "MyFamilyTableVCell.h"
@interface MyFamilyView ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,weak) IBOutlet UIView *contView;

@property(nonatomic,weak) IBOutlet TYMBaseTableView *tableView;

@property(nonatomic,strong) FamilyService *service;
@end

@implementation MyFamilyView


-(void)awakeFromNib{
    [super awakeFromNib];
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([MyFamilyTableVCell  class]) bundle:nil] forCellReuseIdentifier:[MyFamilyTableVCell identifier]];

    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([MyFamilyApplyCell  class]) bundle:nil] forCellReuseIdentifier:[MyFamilyApplyCell identifier]];
    
}

-(FamilyService*)service{
    if (_service == nil) {
        _service = [[FamilyService alloc] init];
    }
    return _service;
}

-(void)setApplysArray:(NSMutableArray *)applysArray{
    _applysArray = applysArray;
    [self.tableView reloadData];
}

-(void)setFamilysArry:(NSArray *)familysArry{
    _familysArry = familysArry;
    [self.tableView reloadData];
}

-(IBAction)cancelAction:(id)sender{
    
    [self removeFromSuperview];
}

//取消申请
-(void)cancelApplesWithAppld:(NSInteger)applyid withOptionRow:(NSInteger)row{
    
    TYMWeak(self);
    [ShowAlertView showAlertViewTitle:@"提示" message:@"确定要取消申请吗？" actionBtnTitle:@"确定" hiddenCancel:NO completion:^(NSInteger actionType) {
        if (actionType) {
           [weakself cancelApplyAppld:applyid withOptionRow:row];
        }
    }];
}

-(void)cancelApplyAppld:(NSInteger)applyid withOptionRow:(NSInteger)row{
    TYMWeak(self);
    [self.service getFamilyuserCancelapplyApplyId:applyid completion:^(NSError * _Nonnull error, id  _Nonnull responObjec) {
        if (!error) {
            [WHToast showMessage:@"取消申请成功" duration:2 finishHandler:^{
                if (weakself.applysArray.count) {
                    [weakself.applysArray removeObjectAtIndex:row];
                    [weakself.tableView deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:row inSection:0]] withRowAnimation:UITableViewRowAnimationLeft];
                    [weakself.tableView reloadData];
                }
                
                if (self.applysArray.count ==0 && self.familysArry.count ==0 ) {
                    [weakself cancelAction:nil];
                }
            }];
        }
    }];
}

#pragma  mark - tableView 代理
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return self.applysArray.count;
    }else if(section == 1){
        return self.familysArry.count;
    }
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [MyFamilyTableVCell cellForHeight];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        MyFamilyApplyCell *cell = [tableView dequeueReusableCellWithIdentifier:[MyFamilyApplyCell identifier] forIndexPath:indexPath];
        cell.applyModel = self.applysArray[indexPath.row];
        cell.cancelApplyBlock = ^(NSInteger applyID) {
            [self cancelApplesWithAppld:applyID withOptionRow:indexPath.row];
        };
        return cell;
    }else{
         MyFamilyTableVCell *cell = [tableView dequeueReusableCellWithIdentifier:[MyFamilyTableVCell identifier] forIndexPath:indexPath];
         cell.familyModel = self.familysArry[indexPath.row];
        
        if (!self.seleFamilyIndexBlock) {
            if (cell.familyModel.familyId == [UserTool getCurrentFamily].familyId) {
                cell.currentLab.hidden = NO;
                cell.righImage.hidden = YES;
            }else{
                cell.currentLab.hidden = YES;
                cell.righImage.hidden = NO;
            }
        }
         return cell;
    }
    return nil;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
     [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section) {
        if (self.seleFamilyIndexBlock) {
            self.seleFamilyIndexBlock(indexPath.row);
        }
    }
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

-(void)dealloc{
    
}

@end
